package org.example.service.post.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.annotation.LogTrace;
import org.example.dao.dao.post.PCommentDao;
import org.example.dao.dao.post.PPostDao;
import org.example.dao.dao.post.PReplyDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.commom.UserInfo;
import org.example.dao.dto.post.PCommentDto;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.post.PComment;
import org.example.dao.entity.post.PPost;
import org.example.exception.ParamLossErrorException;
import org.example.service.post.CommentService;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.redis.RedisUtils;
import org.example.utils.post.comment.ExtendCommentService;
import org.example.utils.post.comment.RedisCommentService;
import org.example.utils.user.RedisUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 22449
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private RedisCommentService redisCommentService;

    @Resource
    private PReplyDao pReplyDao;


    @Resource
    private IdWorker idWorker;


    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PCommentDao pCommentDao;

    @Resource
    private UUserDao uUserDao;

    @Resource
    private PPostDao pPostDao;

    @Resource
    private ExtendCommentService extendCommentService;

    @Resource
    private RedisUserService redisUserService;

    @Override
    @LogTrace
    public PCommentDto commentPost(Long postId, String content) {
        PPost post = pPostDao.selectById(postId);
        if (post == null) {
            throw new ParamLossErrorException("post不存在，id为:" + postId);
        }

        PCommentDto pCommentDto = new PCommentDto();
        UserDto userDto = contextUtil.getLoginUser();
        Long userId = userDto.getUser().getId();
        Long id = idWorker.nextId();
        PComment pComment = new PComment(id, userId, postId, content);
        pCommentDao.insert(pComment);
        pCommentDto.setPComment(pComment);
        pCommentDto.setUserInfo(new UserInfo(userDto));


        //添加待接收队列
        Long receiverId = post.getUserId();
        redisUserService.addUserCommentNoReadList(receiverId.toString(),pComment.getId().toString());
        redisUserService.addUserCommentReadList(receiverId.toString(),pComment.getId().toString());
        return pCommentDto;
    }

    @Override
    @LogTrace
    public IPage<PCommentDto> getCommentByPost(Long postId, Integer start, Integer limit) {
        LambdaQueryWrapper<PComment> lambdaQueryWrapper = extendCommentService.getLambdaQueryWrapper();
        lambdaQueryWrapper.eq(PComment::getPostId, postId);
        IPage<PComment> pPosts = pCommentDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return extendCommentService.getPCommentDtoIpage(pPosts);
    }

    @Override
    @LogTrace
    public IPage<PCommentDto> getCommentByMine(Integer limit, Integer start) {
        Long userId = contextUtil.getId();
        LambdaQueryWrapper<PComment> lambdaQueryWrapper = extendCommentService.getLambdaQueryWrapper();
        lambdaQueryWrapper.eq(PComment::getUserId, userId);
        IPage<PComment> pPosts = pCommentDao.selectPage(new Page<>(start, limit), lambdaQueryWrapper);
        return extendCommentService.getPCommentDtoIpage(pPosts);
    }


    @Override
    @LogTrace
    public Long likeComment(Long id) {
      return redisCommentService.likeComment(id.toString());
    }

    @Override
    @LogTrace
    public PCommentDto updateComment(Long id, String content) {
        UserDto userDto = contextUtil.getLoginUser();
        PCommentDto pCommentDto = new PCommentDto();
        PComment pComment = pCommentDao.selectById(id);
        if (pComment == null) {
            throw new ParamLossErrorException("不存在该评论");
        }
        pComment.setContent(content);
        pComment.setUpdateTime(new Date());
        pCommentDao.updateById(pComment);
        pCommentDto.setPComment(pComment);
        pCommentDto.setUserInfo(new UserInfo(userDto));
        return pCommentDto;
    }

    @Override
    @LogTrace
    public void deleteComment(Long id) {
        PComment pComment = pCommentDao.selectById(id);
        if (pComment == null) {
            throw new ParamLossErrorException("不存在该评论");
        }
        pComment.setUpdateTime(new Date());
        pComment.setIsValid(0);
        pCommentDao.updateById(pComment);
    }

    @Override
    @LogTrace
    public List<PCommentDto> getCommentByToMine(Integer isread, Long limit, Long start) {
        List<String> set = redisUserService.getCommentByToMine(isread,limit,start);
        if(set==null||set.size()==0){
            return null;
        }
        LambdaQueryWrapper<PComment> lambdaQueryWrapper = extendCommentService.getLambdaQueryWrapper();
        lambdaQueryWrapper.in(PComment::getId, set);
        List<PComment> pPosts = pCommentDao.selectList(lambdaQueryWrapper);


        List<PCommentDto> list = new ArrayList<>();
        for (PComment comment : pPosts) {
            PCommentDto commentDto = extendCommentService.createCommentDto(comment);
            list.add(commentDto);
        }
        return list;
    }


}
